/**
 * JAVACC DEMO 1.0
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.passport.common.DesUtils;
import com.apache.passport.common.Native2AsciiUtils;
import com.apache.passport.common.PassPortConst;
import com.apache.passport.common.PassportHelper;
import com.apache.passport.entity.Token;
import com.apache.passport.entity.UctUser;
import com.apache.passport.manager.UctUserManager;

import java.util.ArrayList;

/**
 * description:  更新用户中文名
 * 此操作必须为注册用户<br>
 * 需要传递参数有tokenId,userEname,userCname,ptsidg
 * 需要调用PassportHelper.getInstance().SHA1(ArrayList<String> lst)对数据进行加密处理
 *
 * @author Hou Dayu 创建时间：2016-3-3
 */
public class UpdateCnamePlugin implements PluginConnector {

    protected IDao uctUserDao;

    private UctUserManager uctUserManager;

    public void setUctUserDao(IDao uctUserDao) {
        this.uctUserDao = uctUserDao;
    }

    public void setUctUserManager(UctUserManager uctUserManager) {
        this.uctUserManager = uctUserManager;
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see PluginConnector#execute(ParamsVo)
     */
    @Override
    public Object execute(ParamsVo paramsvo) throws Exception {
        ResultEntity result = new ResultEntity();
        result.setResult("true");
        String tokenId = (String) paramsvo.getParams("tokenId");
        String userEname = (String) paramsvo.getParams("userEname");
        String userCname = (String) paramsvo.getParams("userCname");
        String ptsidg = (String) paramsvo.getParams("ptsidg");
        ArrayList<String> lst = new ArrayList<String>();
        lst.add(tokenId);
        lst.add(userEname);
        lst.add(userCname);
        String macPtsidg = PassportHelper.getInstance().SHA1(lst);
        Token token = PassPortConst.getToken(tokenId);

        if (Validator.isEmpty(ptsidg)) {
            result.setEntity("F");
            result.setMessage("校验参数缺失");
            return result;
        }

        if (!ptsidg.equals(macPtsidg)) {
            result.setEntity("F");
            result.setMessage("系统检测到信息被篡改");
            return result;
        }

        //登录之后才可修改
        if (Validator.isEmpty(token)) {
            result.setEntity("F");
            result.setMessage("用户未登录或认证已过期");
            return result;
        }

        if (Validator.isNull(userEname)) {
            result.setEntity("F");
            result.setMessage("用户名不能为空");
            return result;
        }

        String desEname = DesUtils.getInstance().decrypt(token.getUserEname());
        if (!userEname.equals(desEname)) {
            result.setEntity("F");
            result.setMessage("用户名不一致");
            return result;
        }
        if (Validator.isNull(userCname)) {
            result.setEntity("F");
            result.setMessage("用户名不能为空");
            return result;
        }
        userCname = Native2AsciiUtils.getInstance().ascii2Native(userCname);

        UctUser obj = (UctUser) getInfoByEname(userEname);
        if (Validator.isEmpty(obj)) {
            result.setEntity("F");
            result.setMessage("用户不存在,请确认!");
            return result;
        }
        obj.setUserCname(userCname);
        paramsvo.setObj(obj);
        uctUserManager.editInfo(paramsvo);
        result.setEntity("T");
        result.setMessage("用户名修改成功");
        return result;
    }

    /**
     * description:  根据ename查询用户信息
     *
     * @param userEname
     * @return
     */
    public Object getInfoByEname(String userEname) {
        String sql = uctUserDao.getSql(3);
        MethodParam param = new MethodParam("ByUserEname", "", sql,
                "com.apache.passport.entity.UctUser");
        param.setParams("userEname", userEname);
        return uctUserDao.selectSingle(param);
    }

    public IDao getUctUserDao() {
        return uctUserDao;
    }

    public UctUserManager getUctUserManager() {
        return uctUserManager;
    }

}
